package com.linkdood.app.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.linkdood.app.constants.SettingConstants;
import com.linkdood.app.domain.Setting;
import com.linkdood.app.mapper.SettingMapper;
import com.linkdood.app.service.SettingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 设置
 * @author YunJ
 */
@Slf4j
@Service
public class SettingServiceImpl extends ServiceImpl<SettingMapper, Setting> implements SettingService {

    /**
     * 获取数据库的版本号
     */
    @Override
    @Transactional(readOnly = true, rollbackFor = Exception.class)
    public String getDbVersion() {
        List<Setting> settings = lambdaQuery().eq(Setting::getName, SettingConstants.SETTING_DB_VERSION).list();
        if (settings.isEmpty()) {
            return "";
        }
        return settings.get(0).getValue();
    }

    /**
     * 更新数据库版本
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateDbVersion(String version) {
        List<Setting> settings = lambdaQuery().eq(Setting::getName, SettingConstants.SETTING_DB_VERSION).list();
        if (!settings.isEmpty()) {
            Setting setting = settings.get(0);
            setting.setValue(version);
            updateById(setting);
        } else {
            Setting setting = new Setting();
            setting.setName(SettingConstants.SETTING_DB_VERSION);
            setting.setValue(version);
            save(setting);
        }
    }

    /**
     * 获取增量更新时，用户的 changeVersion
     */
    @Override
    public String getUserChangeVersion() {
        Setting setting = lambdaQuery().eq(Setting::getName, SettingConstants.SETTING_USER_CHANGE_VERSION).one();
        if (setting == null) {
            return "1";
        }
        return setting.getValue();
    }

    /**
     * 获取增量更新时，组织的 changeVersion
     */
    @Override
    public String getOrgChangeVersion() {
        Setting setting = lambdaQuery().eq(Setting::getName, SettingConstants.SETTING_ORG_CHANGE_VERSION).one();
        if (setting == null) {
            return "1";
        }
        return setting.getValue();
    }

    /**
     * 更新部门版本号
     */
    @Override
    public void updateOrgChangeVersion(String version) {
        Setting setting = lambdaQuery().eq(Setting::getName, SettingConstants.SETTING_ORG_CHANGE_VERSION).one();
        if (setting == null) {
            setting = new Setting();
            setting.setName(SettingConstants.SETTING_ORG_CHANGE_VERSION);
            setting.setValue(version);
            save(setting);
        } else {
            setting.setValue(version);
            updateById(setting);
        }
    }

    /**
     * 更新用户版本号
     */
    @Override
    public void updateUserChangeVersion(String version) {
        Setting setting = lambdaQuery().eq(Setting::getName, SettingConstants.SETTING_USER_CHANGE_VERSION).one();
        if (setting == null) {
            setting = new Setting();
            setting.setName(SettingConstants.SETTING_USER_CHANGE_VERSION);
            setting.setValue(version);
            save(setting);
        } else {
            setting.setValue(version);
            updateById(setting);
        }
    }
}
